<template>
  <div class="shop_main">
    <el-card>
      <el-form ref="formRef" :model="form" style="width: 100%">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="类目名称" prop="name">
              <el-input v-model="form.name" style="width: 100%" placeholder="请输入名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="" prop="name">
              <el-button :icon="Search" type="primary" @click="search">查 询</el-button>
              <el-button :icon="Refresh" @click="reset">重 置</el-button>
              <el-button :icon="Plus" type="primary" @click="openEdit">新 增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    &nbsp;
    <el-card>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="name" label="类目名称" align="center"></el-table-column>
        <el-table-column prop="total" label="商品总数" align="center"></el-table-column>
        <el-table-column prop="exportNum" label="出库数" align="center"></el-table-column>
        <el-table-column prop="memo" label="备注" align="center">
          <template #default="{ row }">
            {{ row.memo || '--' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"  align="center">
          <template #default="{ row }">
            {{ formattedDateTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作" align="center">
          <template #default="{ row }">
            <el-button link type="primary" :icon="EditPen" @click="openEdit(row)">编辑</el-button>
            <el-button @click="delCallback(row.id)" link :icon="Delete" style="color: #f56c6c">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      &nbsp;
      <el-pagination
        style="display: flex; justify-content: flex-end"
        background
        :current-page="pagenation.page"
        :page-size="pagenation.limit"
        :total="pagenation.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    &nbsp;

    <Edit v-model="showEdit" :info="currentEdit" @callback="loadData" />
  </div>
</template>

<script setup>
  import { getTypePage, deleteType } from '@/server/type';
  import { ref, onMounted } from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import { Search, Refresh, Plus, EditPen, Delete } from '@element-plus/icons-vue';
  import { useDateFomatter } from '@/hooks/formatter';
  import Edit from './edit.vue';

  const { formattedDateTime } = useDateFomatter();
  const form = ref({
    name: '',
    type: '',
  });
  const pagenation = ref({
    page: 1,
    limit: 10,
    total: 0,
  });
  const tableData = ref([]);

  const showEdit = ref(false);
  const currentEdit = ref({});

  const handleSizeChange = size => {
    pagenation.value.limit = size;
    loadData();
  };
  const handleCurrentChange = page => {
    pagenation.value.page = page;
    loadData();
  };

  function openAdd() {
    currentEdit.value = {};
    showEdit.value = true;
  }
  function openEdit(row) {
    currentEdit.value = row;
    showEdit.value = true;
  }

  function closeEdit() {
    showEdit.value = false;
  }

  async function delCallback(id) {
    ElMessageBox.confirm('确认删除该类目吗？删除后绑定的商品数据也会被删除', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
      customClass: 'custom-message-box',
      customStyle: { fontSize: '50px' },
    }).then(async () => {
      const res = await deleteType({ id });
      if (res.code == 200) {
        ElMessage.success('操作成功');
        loadData();
      }
    });
  }

  function search() {
    loadData();
  }
  function reset() {
    form.value.name = '';
    form.value.type = '';
    loadData();
  }

  async function loadData() {
    const searchModel = { ...form.value, ...pagenation.value };
    const res = await getTypePage(searchModel);
    if (res.code === 200) {
      tableData.value = res.data.rows;
      pagenation.value.total = res.data.total;
    }
  }

  onMounted(() => {
    loadData();
  });
</script>

<style scoped>
  .shop_main {
    width: 100%;
    height: 100%;
    position: relative;
  }
</style>
